package WWW::OpenX::API::RPC::XML::Client;
use strict;
use warnings;
use Moose;
use MooseX::NonMoose;
use namespace::autoclean;
use DateTime;

our $VERSION = sprintf('0.1.%d', q|$Rev: 8 $| =~ /(\d+)/);

extends 'RPC::XML::Client';

sub FOREIGNBUILDARGS {
    my $package = shift;
    my %options = (@_);

    return ($options{'uri'});
}

sub implode {
    my $self = shift;
    my $response = shift;
    my $imploded;

    if(blessed($response) eq 'RPC::XML::array') {
        $imploded = [];
        push(@$imploded, $self->implode($_)) for(@$response);
    } elsif(blessed($response) eq 'RPC::XML::struct') {
        $imploded = {};
        $imploded->{$_} = $self->implode($response->{$_}) for(keys(%$response));
    } elsif(blessed($response) eq 'RPC::XML::datetime_iso8601') {
        my $v = $response->value;
        if($v =~ /(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})/) {
            $imploded = DateTime->new(
                year => $+{year},
                month => $+{month},
                day => $+{day},
                hour => $+{hour},
                minute => $+{minute},
                second => $+{second}
                );
        } else {
            die WWW::OpenX::API::Exception::ImplodeError->new(message => sprintf('Could not convert ISO8601 date (%s) to DateTime object during implosion', $v));
        }
    } else {
        $imploded = $response->value;
    }
    return $imploded;
}

sub send_request_and_implode {
    my $self = shift;
    
    return $self->implode($self->send_request(@_));
}
    
__PACKAGE__->meta->make_immutable();

1;
=pod ------------------------------------------------------------------------------------------

=head1 WWW::OpenX::API::RPC::XML::Client

A Moose wrapper for RPC::XML::Client. Uses MooseX::NonMoose to get the Moose in the house.
and supplies a few additional functions to make life a bit easier.

=head1 SYNOPSIS

    my $client = WWW::OpenX::API::RPC::XML::Client->new(uri => 'http://......');
    # now use it just as you would RPC::XML::Client

=head1 SUBROUTINES/METHODS

=head2 implode($xmlRpcResponse)

An XMLRPC response is built up out of objects in the RPC::XML namespace, and generally speaking
are hard to use directly. This method will take care of taking an XMLRPC response and imploding
the data inside into a regular perl structure. Will return a scalar, hashref, or arrayref, 
depending on the original format of the response. 

=head2 send_request_and_implode(@args)

This function is the equivalent of doing

    return $client->implode($client->send_request(@args))

=head1 AUTHOR

Ben van Staveren C<< madcat at cpan.org >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-www-openx-api at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-OpenX-API>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc WWW::OpenX::API::RPC::XML::Client

=head1 SEE ALSO

L<WWW::OpenX::API>, L<RPC::XML::Client>

=head1 LICENSE AND COPYRIGHT

Copyright 2010 Ben van Staveren.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991 or at your option
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

A copy of the GNU General Public License is available in the source tree;
if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=cut ------------------------------------------------------------------------------------------


